/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ania2.engine.utils;

import ania2.BoardPosition;

/**
 *
 * @author pawel
 */
public class TranspositionTable {
    
    private final int TABLE_SIZE = 131072 /*111077*/;
    private TranspositionTableEntry[] table;
    private int collisionCounter;
    private int hitCounter;

    public TranspositionTable() {
        table = new TranspositionTableEntry[TABLE_SIZE];
    }
    
    
    public TranspositionTableEntry lookUp(BoardPosition position) {
        int key = Math.abs(position.getHashCode()) % TABLE_SIZE;
        TranspositionTableEntry entry = table[key];
        if(entry == null)
            return null;
        if(position.getLockCode()==entry.getLockCode()) {
            hitCounter++;            
            return entry;
        }
        return null;
    }
    
    public void store(BoardPosition position, TranspositionTableEntry entry) {
        int key = Math.abs(position.getHashCode()) % TABLE_SIZE;
        if(table[key]!=null) {
            if(table[key].getLockCode() != entry.getLockCode())
                collisionCounter++;
            if(entry.getDepth()<table[key].getDepth())
                return;
        }
        table[key] = entry;
    }
    
    public boolean containsKey(int hashCode) {
        int key = Math.abs(hashCode) % TABLE_SIZE;
        return (table[key]==null) ? false : true;
    }

    public int getCollisionCounter() {
        return collisionCounter;
    }

    public int getHitCounter() {
        return hitCounter;
    }
    
    public int getNumberOfEntries() {
        int entries=0;
        for(int i=0; i<TABLE_SIZE; i++)
            if(table[i] != null)
                entries++;
        return entries;
    }
}
